package com.zjhcsoft.shop.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.zjhcsoft.common.dao.MybatisCommonDao;
import com.zjhcsoft.common.object.GridObj;
import com.zjhcsoft.common.object.OrderObj;
import com.zjhcsoft.common.object.PageObj;
import com.zjhcsoft.shop.dao.ProductDao;
import com.zjhcsoft.shop.dao.UserDao;
import com.zjhcsoft.shop.model.Product;
import com.zjhcsoft.shop.model.User;

@SuppressWarnings("unchecked")
@Repository
public class ProductDaoImpl extends MybatisCommonDao<Product, Long> implements ProductDao{
	
	private static final Logger logger = Logger.getLogger(ProductDaoImpl.class);
	
	public ProductDaoImpl(){
		super.tableName="product";
		super.primaryKeyName="id";
	}
	/**
	 * 保存一件衣服产品对象
	 */
	public void  saveOrUpdate(Product  entity){
		 try {
			super.saveOrUpdate(entity);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	/**
	 * 根据ID获取产品对象
	 * @param id
	 * @return
	 */
	public Product load(Long id){
		try {
			return super.load(Product.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return null;
	}
	
	/**
	 * 查找前50条
	 * @param kindid 产品归类，1:新品商品，2:精品推荐，3:特价商品
	 * @param size
	 * @param order
	 * @return
	 */
	public List<Product> findProductList(Integer kindid, Integer size, String order){
		Product product=new Product();
		product.setKindid(kindid);
		PageObj pageObj = new PageObj("0",size.toString());
		OrderObj orderObj = new OrderObj(order,"desc");
		try {
			GridObj result = super.findByPage(product, pageObj, orderObj);
			return (List<Product>) result.getRoot();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return null;
	}
}
